﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterAttackSensor : MonoBehaviour
{
    public Hashtable targetTable;        
    public int targetNum = 0;

    private void Awake()
    {
        targetTable = new Hashtable();        
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnTriggerEnter2D(Collider2D collision) {
        GameObject target = collision.gameObject;
        if (target.tag == "Player" || target.tag == "iron_block")
        {
            targetTable.Add(target.GetHashCode(), target);
            targetNum++;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        GameObject target = collision.gameObject;
        if (target.tag == "Player" || target.tag == "iron_block")
        {
            targetTable.Remove(target.GetHashCode());
            targetNum--;
        }
    }

    public void hurtTargetTable(int hurt) {
        List<GameObject> tmpList = new List<GameObject>();
        foreach (DictionaryEntry entry in targetTable) {
            GameObject target = entry.Value as GameObject;
            //target.SendMessage("GetHurt", 10);
            tmpList.Add(target);
        }
        for (int i = 0; i < tmpList.Count; ++i) {
            tmpList[i].SendMessage("GetHurt", hurt);
        }
    }
        
}
